#include "SimDriverBoard.h"
#include "DriverModule/DriverManager.h"
#include "tools/TcpServer/TcpServer.h"
#include "ProtocolModule/XXXSession.h"
#include "ProtocolModule/XXXMsgManager.h"

#include <iostream>

SimDriverBoard& SimDriverBoard::Instance()
{
    return m_instance;
}


SimDriverBoard::SimDriverBoard()
{

}

SimDriverBoard::~SimDriverBoard()
{
    Exit();
}

// 注意：各个服务模块进行初始化，有先后顺序
bool SimDriverBoard::Init()
{
    return RegisterAndInitMgrs();
}

void SimDriverBoard::Exit()
{
    DestroyManagers();
}

bool SimDriverBoard::Start()
{
    // 初始化通讯server。 本地的所有服务都准备好了，才启动通讯，响应外部请求。
    return StartServer();
}

bool SimDriverBoard::RegisterAndInitMgrs()
{
    bool ret = false;

// 1、注册服务模块，各个服务模块可能存在注册的先后顺序
    ret = RegisterManager<DriverManager>();                     // 注册驱动服务模块
    if (!ret)
    {
        std::cout << "Register Manager failed! name:" << 
            typeid(DriverManager).name() << std::endl;
        return ret;
    }

    ret = RegisterManager<XXXMsgManager>();                     // 注册协议处理服务模块
    if (!ret)
    {
        std::cout << "Register Manager failed! name:" << 
            typeid(XXXMsgManager).name() << std::endl;
        return ret;
    }


// 2、各个服务模块，统一进行初始化（按照顺序）
    return InitManagers();
}



bool SimDriverBoard::StartServer()
{
    const static short TCP_SERVER_PORT = 12345;                 // tcp服务器绑定的端口

    TcpServerPtr tcpServer = std::make_shared<TcpServer>(TCP_SERVER_PORT);
    tcpServer->RegisterSessionType<XXXSession>();               // 注册具体的连接处理对象， XXX协议的处理对象类型
    m_server = tcpServer;
    m_server->Start();

    return true;
}


SimDriverBoard SimDriverBoard::m_instance;
